# Maintainer: MVZ Labor Ludwigsburg <edv@mvz-labor-lb.de>
# vim: ft=bash

# Inspired by https://github.com/archlinux/svntogit-community/blob/master/mupdf/trunk/PKGBUILD

_realname=mupdf
pkgbase=mingw-w64-${_realname}
pkgname=(
  "${MINGW_PACKAGE_PREFIX}-${_realname}-libmupdf"
  "${MINGW_PACKAGE_PREFIX}-${_realname}-mupdf"
  "${MINGW_PACKAGE_PREFIX}-${_realname}-mupdf-tools"
)
pkgver=1.21.0
pkgrel=2
pkgdesc='Lightweight PDF and XPS viewer (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://mupdf.com/"
license=('spdx:AGPL-3.0-or-later')
depends=(
  "${MINGW_PACKAGE_PREFIX}-freetype"
  "${MINGW_PACKAGE_PREFIX}-harfbuzz"
  "${MINGW_PACKAGE_PREFIX}-jbig2dec"
  "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
  "${MINGW_PACKAGE_PREFIX}-openjpeg2"
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-gumbo-parser"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
)
source=("https://mupdf.com/downloads/archive/${_realname}-${pkgver}-source.tar.gz"
        "001-Makerules.patch"
)
noextract=("${_realname}-${pkgver}-source.tar.gz")
sha256sums=('21fef5b2196b60dc4cf9358f917c2255b1a263a05391129e9ca76a7b5c5923ce'
            '801bda519b357bcffef68ee7581912747a6fcf04e8b5284533a20af9e1e5d9fa')

prepare() {
  tar -xf "${_realname}-${pkgver}-source.tar.gz" || true

  cd "${_realname}-${pkgver}-source"

  # remove bundled packages, we want our system libraries
  rm -rf thirdparty/{freeglut,freetype,harfbuzz,jbig2dec,libjpeg,openjpeg,zlib}

  patch --forward --strip=1 --input="${srcdir}/001-Makerules.patch"
}

build() {
  cd "${_realname}-${pkgver}-source"

  make USE_SYSTEM_LIBS=yes HAVE_GLUT=no HAVE_WIN32=yes WINDRES=windres prefix=${MINGW_PREFIX} \
    DESTDIR=install build=release libs apps install
}

package_libmupdf() {
  pkgdesc='Library for Lightweight PDF and XPS viewer (mingw-w64)'
  depends=()

  cd "${_realname}-${pkgver}-source"

  install -D -m0644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/mupdf-libmupdf" README COPYING CHANGES
  cp -r install${MINGW_PREFIX}/{include,lib} "${pkgdir}${MINGW_PREFIX}/"
}

package_mupdf() {
  pkgdesc='Lightweight PDF and XPS viewer (mingw-w64)'

  cd "${_realname}-${pkgver}-source"

  install -D -m0755 install${MINGW_PREFIX}/bin/mupdf-w32 "${pkgdir}${MINGW_PREFIX}/bin/mupdf.exe"
  install -D -m0644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/mupdf" README COPYING CHANGES
  install -D -m0644 -t "${pkgdir}${MINGW_PREFIX}/share/man/man1" install${MINGW_PREFIX}/share/man/man1/mupdf.1*
  install -d -m0755 "${pkdir}${MINGW_PREFIX}/share/doc"
  cp -r install${MINGW_PREFIX}/share/doc/mupdf "${pkgdir}${MINGW_PREFIX}/share/doc/"
}

package_mupdf-tools() {
  pkgdesc='Tools for Lightweight PDF and XPS viewer (mingw-w64)'

  cd "${_realname}-${pkgver}-source"

  install -D -m0755 -t "${pkgdir}${MINGW_PREFIX}/bin" install${MINGW_PREFIX}/bin/mu{raster,tool}
  install -D -m0644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/mupdf-tools" README COPYING CHANGES
  install -D -m0644 -t "${pkgdir}${MINGW_PREFIX}/share/man/man1" install${MINGW_PREFIX}/share/man/man1/mutool.1*
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-${_realname}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
